{% extends "base.html" %}
{% load i18n %}
{% load event_tags %}

{% block content %}
    {{ block.super }}
    <h2> {{ name }}</h2>
    <hr/>
    <p>{% trans "Your current API key is" %} <code>{{ key.key }}</code></p>
    <p>{% trans "Your current API Authorization Header value is" %} <code>Token {{ key.key }}</code></p>
    <p>{% trans "Has your key been exposed? Are you ready for a new one?" %}</p>
    <form action="{% url 'api_v2_key' %}" method="post" class="inline-form">
        {% csrf_token %}
        {{ form.as_p }}
        <input class="btn btn-primary" type="submit" value="{% trans "Generate New Key" %}"/>
    </form>
    <hr/>
    {% if API_TOKEN_AUTH_ENDPOINT_ENABLED %}
    <p>{% trans "Alternatively, you can use /api/v2/api-token-auth/ to get your token. Example:" %}</p>
    <pre>
curl -X POST -H 'content-type: application/json' {% if request.is_secure %}https{% else %}http{% endif %}://{{ request.META.HTTP_HOST }}/api/v2/api-token-auth/ -d '{"username": "&lt;YOURUSERNAME&gt;", "password": "&lt;YOURPASSWORD&gt;"}'</pre>
    {% endif %}
    <p>{% trans "To use your API Key you need to specify an Authorization header. Example:" %}</p>
    <pre>
# As a header
# Format is ``Authorization: Token &lt;api_key&gt;
Authorization: Token {{ key.key }}
	</pre>
    <p>{% trans "Here is a simple python example against the /users endpoint" %}</p>
    <pre>
import requests

url = '{% if request.is_secure %}https{% else %}http{% endif %}://{{ request.META.HTTP_HOST }}/api/v2/users/'
headers = {'content-type': 'application/json',
           'Authorization': 'Token {{ key.key }}'}
r = requests.get(url, headers=headers, verify=True) # set verify to False if ssl cert is self-signed

for key, value in r.__dict__.items():
  print(f"'{key}': '{value}'")
  print('------------------')
	</pre>
{% endblock %}
